<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 公司部门逻辑
 */
class Branch extends AdminBase
{

    /**
     * 获取公司部门列表
     */
    public function getBranchList($where = [], $field = 'a.*,b.name', $order = '', $paginate=0)
    {
        if(!empty($field)){
            $this->modelBranch->alias('a');
        
            $join = [
                    [SYS_DB_PREFIX . 'company b', 'a.company_id = b.id'],
                ];
        
            $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE]; 
            return $this->modelBranch->getList($where, $field,'a.sort desc', $paginate,$join);
        }else{
            return $this->modelBranch->getList($where, $field, $order,false);
        }

    }
    
    /**
     * 获取部门列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.branch_name|b.name'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['company_id']) && $where['a.company_id'] = ['eq', $data['company_id']];
        
        return $where;
    }

    /**
     * 导出部门列表
     */
    public function exportBranchList($where = [], $field = 'a.*,b.name', $order = '')
    {
        
        $list = $this->getBranchList($where, $field, $order,false);
        
        $titles = "部门名,部门负责人,所属公司,注册时间";
        $keys   = "branch_name,principal,name,create_time";
        
        action_log('导出', '导出部门列表');
        
        export_excel($titles, $keys, $list, '部门列表');
    }
    
    /**
     * 部门添加
     */
    public function branchAdd($data = [])
    {
        
        $validate_result = $this->validateBranch->scene('add')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBranch->getError()];
        }
        
        $url = url('branchList');
        
        // $data['nickname']  = $data['username'];
        // $data['leader_id'] = MEMBER_ID;
        // $data['is_inside'] = DATA_NORMAL;
        
        $result = $this->modelBranch->setInfo($data);
        
        $result && action_log('新增', '新增部门，name：' . $data['branch_name']);
        
        return $result ? [RESULT_SUCCESS, '部门添加成功', $url] : [RESULT_ERROR, $this->modelbranch->getError()];
    }
    
    /**
     * 获取部门信息
     */
    public function getBranchInfo($where = [])
    {
        
        return $this->modelBranch->getInfo($where);
    }

    /**
     * 部门编辑
     */
    public function branchEdit($data = [])
    {
        
        $validate_result = $this->validateBranch->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBranch->getError()];
        }
        
        $url = url('branchList');
        // var_dump($data);exit;
        $result = $this->modelBranch->setInfo($data);
        
        $result && action_log('编辑', '编辑部门，：name:' . $data['branch_name']);
        
        return $result ? [RESULT_SUCCESS, '部门编辑成功', $url] : [RESULT_ERROR, $this->modelBranch->getError()];
    }
    
    /**
     * 设置部门信息
     */
    public function setMemberValue($where = [], $field = '', $value = '')
    {
        
        return $this->modelMember->setFieldValue($where, $field, $value);
    }
    
    /**
     * 部门删除
     */
    public function branchDel($where = [])
    {
        
        $url = url('branchList');
        
        if (SYS_ADMINISTRATOR_ID == $where['id'] || MEMBER_ID == $where['id']) {
            
            return [RESULT_ERROR, '天神和自己不能删除哦~', $url];
        }
        
        $result = $this->modelbranch->deleteInfo($where);
                
        $result && action_log('删除', '删除部门，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '部门删除成功', $url] : [RESULT_ERROR, $this->modelbranch->getError(), $url];
    }
}
